﻿using System;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;

using GroupChallenge.Controls;
using GroupChallenge.Helpers;
using GroupChallenge.Web;
using GroupChallenge.Web.Services;

namespace GroupChallenge
{
    public partial class PlayerImagesView : Page
    {
        #region Initialization

        public PlayerImagesView()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.PlayerImagesTitle;

            Loaded += new RoutedEventHandler(PlayerImagesView_Loaded);
        }

        private void PlayerImagesView_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshPlayerImageList();
        }

        #endregion                              // Initialization

        #region UI Events

        private void RefreshListButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            RefreshPlayerImageList();
        }

        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            RefreshPlayerImageList();
        }

        private void DeleteImageButton_Click(object sender, RoutedEventArgs e)
        {
            int deleteImageId = 0;

            if (int.TryParse(ImageIdToDeleteTextBox.Text, out deleteImageId) &&
                deleteImageId > 0)
            {
                MainApp.ViewModel.DeletePlayerImage(deleteImageId);
            }
        }

        #endregion                              // UI Events

        #region Private Members

        private void RefreshPlayerImageList()
        {
            Uri uri = new Uri(Utils.GetHostUrl(true) + "GroupChallenge/GroupChallenge.svc");

            GroupChallengeDomainContext groupChallengeDomainContext = new GroupChallengeDomainContext();

            var query = groupChallengeDomainContext.GetPlayerImageListQuery();

            groupChallengeDomainContext.Load(query, CompletedRefreshPlayerImageList, true);
        }

        private void CompletedRefreshPlayerImageList(LoadOperation<PlayerImage> loadOperation)
        {
            if (loadOperation.HasError)
            {
                loadOperation.MarkErrorAsHandled();

                MessageBox.Show("Unable to retrieve the player image list.\r\n\r\nReason: " + loadOperation.Error.Message, "List Load Error", MessageBoxButton.OK);
            }
            else
            {
                if (null != loadOperation.Entities)
                {
                    MainApp.ViewModel.PlayerImages.Clear();
                    PlayerImageGrid.Children.Clear();           // Clear grid or they show up again.

                    string pathReplacement = string.Empty;

                    foreach (PlayerImage entity in loadOperation.Entities)
                    {
                        MainApp.ViewModel.AddPlayerImage(entity);
                    }

                    int row = 0;
                    int col = 0;
                    int maxColumns = 4;

                    PlayerImageGrid.RowDefinitions.Clear();
                    PlayerImageGrid.ColumnDefinitions.Clear();

                    // Add the initial row.
                    RowDefinition rowDefinition = new RowDefinition();
                    PlayerImageGrid.RowDefinitions.Add(rowDefinition);

                    // Create all column definitions.
                    for (int i = 0; i < maxColumns; i++)
                    {
                        ColumnDefinition columnDefinition = new ColumnDefinition();
                        columnDefinition.Width = new GridLength(300);
                        PlayerImageGrid.ColumnDefinitions.Add(columnDefinition);
                    }

                    // Create a grid row and cell until all are displayed.
                    foreach (PlayerImage playerImage in MainApp.ViewModel.PlayerImages)
                    {
                        PlayerImageView playerImageView = new PlayerImageView();
                        playerImageView.DataContext = playerImage;

                        playerImageView.Margin = new Thickness(5);
                        PlayerImageGrid.Children.Add(playerImageView);

                        Grid.SetRow(playerImageView, row);
                        Grid.SetColumn(playerImageView, col);

                        col++;
                        if (col >= maxColumns)
                        {
                            col = 0;
                            row++;

                            rowDefinition = new RowDefinition();
                            rowDefinition.Height = new GridLength(300);
                            PlayerImageGrid.RowDefinitions.Add(rowDefinition);
                        }
                    }
                }
            }
        }

        #endregion                              // Private Members
    }
}